package com.zwps.biz.controller.http;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import com.zwps.biz.controller.common.CommonSystemController;
import com.zwps.biz.api.model.dto.auth.PasswordCaptchaLoginAuthDTO;
import com.zwps.biz.api.model.dto.auth.PasswordLoginAuthDTO;
import com.zwps.biz.api.model.dto.auth.PhoneLoginDTO;
import com.zwps.biz.api.model.vo.auth.AuthInfoVO;
import com.zwps.biz.domain.service.AuthService;
import com.zwps.common.api.validator.Phone;
import com.zwps.common.controller.annotation.Record;
import com.zwps.common.controller.response.message.ResponseDataMessage;
import com.zwps.common.core.enums.LogTypeEnum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 
 * @ClassName: LoginController
 * @Description: 系统登录退出
 * @author lyh
 * @date 2023年5月16日 下午4:27:08
 *
 */
@Tag(name = "登录退出")
@RestController
@RequestMapping("system/auth")
@Validated
public class AuthController extends CommonSystemController {

    @Autowired
    private AuthService authService;

    /**
     * 用户名密码登录（不需要验证码）
     * 
     * @Title: login
     * @Description: 
     * @param loginDTO
     * @return LoginInfoVO
     * @author lyh
     * @date 2023年5月16日 下午4:42:52
     */
    @Operation(summary = "用户名密码登录")
    @PostMapping("login/password")
    @Record(logType = LogTypeEnum.AUTH)
    public AuthInfoVO passwordLogin(@Valid @NotNull @RequestBody PasswordLoginAuthDTO  loginDTO) throws Exception {
        return authService.passwordLogin(loginDTO);
    }

    @Operation(summary = "用户名密码和验证码登录")
    @PostMapping("login/password/captcha")
    @Record(logType = LogTypeEnum.AUTH)
    public AuthInfoVO passwordCaptchaLogin(@Valid @NotNull @RequestBody PasswordCaptchaLoginAuthDTO loginDTO) throws Exception {
        return authService.passwordLogin(loginDTO);
    }

    @Operation(summary = "获取Base64格式的验证码")
    @GetMapping("login/captcha/base64")
    @Record(logType = LogTypeEnum.AUTH)
    public ResponseDataMessage<String> generateCaptchaBase64() {
        return ResponseDataMessage.data(authService.generateCaptchaBase64());
    }

    @Operation(summary = "手机号码登录")
    @PostMapping("login/phone")
    @Record(logType = LogTypeEnum.AUTH)
    public AuthInfoVO phoneLogin(@Valid @NotNull @RequestBody PhoneLoginDTO loginDTO) throws Exception {
        return authService.phoneLogin(loginDTO);
    }

    @Operation(summary = "发送手机短信登录验证码")
    @PostMapping("phone_code/{phone}")
    @Record(logType = LogTypeEnum.AUTH)
    public void generatePhoneCode(@PathVariable("phone") @Phone String phone) throws Exception {
        authService.sendPhoneCode(phone);
    }

    @Operation(summary = "退出登录")
    @DeleteMapping("logout")
    @Record(logType = LogTypeEnum.AUTH)
    public void logout() {
        authService.logout(getRawToken());
    }

    @Operation(summary = "刷新token")
    @Record(logType = LogTypeEnum.AUTH)
    @GetMapping("refresh_token")
    public void refreshToken() {

    }

    @Operation(summary = "第三方使用密钥登录")
    @PostMapping("login/secretKey")
    @Record(logType = LogTypeEnum.AUTH)
    public AuthInfoVO secretKeyLogin(@Valid @NotNull String secretKey) throws Exception {
        return authService.secretKeyLogin(secretKey);
    }

    /**
     * 校验密钥是否过期
     */
    @Operation(summary = "校验密钥是否过期")
    @GetMapping("LoginSecretKey/verification")
    public ResponseDataMessage<Boolean> verification(@Valid @NotNull String code){
        return ResponseDataMessage.data(authService.verification(code));
    }
    public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode("Abcd@123");
        System.out.println(encode);
    }
}
